#!/usr/bin/python
import json , sys
from copy import deepcopy

__author__ = 'meirdrago'


class GroupPulse(object):
        def __init__(self,conf):
                self.conf = conf
                if not "sumfields" in self.conf: self.conf["sumfields"] = {}
                self.Persist = {}
                self.row_pattern = self.new_row_pattern()

        def new_row_pattern(self):
                sum_fields_keys = self.conf['sumfields'].keys()
                non_exist_row = {"count" : 0.0}
                for sumf in sum_fields_keys :
                        non_exist_row[sumf] = {} if self.conf['sumfields'][sumf] == "dist" else 0.0
                return non_exist_row

	def filter_out(self,rd):
		if not "filter" in self.conf: return False
		for k in self.conf["filter"] :
			if rd[k] != self.conf["filter"][k]: return True
		return False

        def in_line(self , rd):
                sum_fields_keys = self.conf['sumfields'].keys()
                for k in self.conf['fields'] :
                        if not k in rd : rd[k] = ""
                for k in sum_fields_keys :
                        if not k in rd : rd[k] = 0.0

		if self.filter_out(rd) : 
			return

                vals = [rd[f] for f in self.conf['fields']]
                _k = self.conf['splitter'].join(vals)

                if not _k in self.Persist : self.Persist[_k] = deepcopy(self.row_pattern)
                self.Persist[_k]["count"] += 1.0                                                          #       count

                for sumf in sum_fields_keys :
                        #       FOR DIST MAKE ASSOCIATIVE ARRAYS
                        if self.conf['sumfields'][sumf] == "dist" :
                                val = rd[sumf]
                                self.Persist[_k][sumf][val] = 1.0 if val not in self.Persist[_k][sumf] else self.Persist[_k][sumf][val] + 1.0
                                continue

                        #       FOR NON DIST FIELDS GET THE FLOAT VALUE OF THE FIELD
                        try :
                                self.Persist[_k][sumf] += float(rd[sumf]) if sumf in rd else 0.0        #       sum of field
                        except:
                                self.Persist[_k][sumf] += 0.0


        def arithmetics(self):
                sum_fields_keys = self.conf['sumfields'].keys()
                if len(sum_fields_keys) == 0 : return
                for k in self.Persist :
                        for sumf in sum_fields_keys :
                                if      self.conf['sumfields'][sumf] == "sum" : self.Persist[k][sumf] = float(self.Persist[k][sumf])
                                elif    self.conf['sumfields'][sumf] == "avg" : self.Persist[k][sumf] /= float(self.Persist[k]["count"])
                                elif    self.conf['sumfields'][sumf] == "dist": self.Persist[k][sumf] = float(len(self.Persist[k][sumf].keys()))


        def mergeWith(self,Master):
                curr = {}
                for k in self.Persist:
                        if k not in curr        : curr[k] = self.Persist[k]
                        else                    :
                                master_count = float(Master[k]["count"])
                                self_count   = float(self.Persist[k]["count"])
                                for field in self.Persist[k] :
                                        curr[k][field] = ((Master[k][field] / master_count) + (self.Persist[k][field]  / self_count)) / 4.0 * (master_count + self_count)
                for k in Master:
                        if k not in curr        : curr[k] = Master[k]
                self.Persist = curr

############################################################
